package at.bartinger.snake;


import java.util.ArrayList;
import java.util.LinkedList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.AudioManager;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

import com.stickycoding.Rokon.RokonActivity;
import com.stickycoding.Rokon.Sprite;
import com.stickycoding.Rokon.Texture;
import com.stickycoding.Rokon.TextureAtlas;
import com.stickycoding.Rokon.TextureManager;
import com.stickycoding.Rokon.Backgrounds.FixedBackground;

public class GraphicActivity extends RokonActivity{

	//SNAKE
	private Game game;
	private Snake snake;
	private Bonus bonus;

	public static int delay = 50;

	private Display display;

	private LinkedList<Sprite> scorelist = new LinkedList<Sprite>();

	//ROKON

	public Texture backgroundTexture;
	public TextureAtlas atlas;
	public Sprite backsprite;

	public FixedBackground background;

	public Texture elementTexture;
	public Sprite elementSprite;


	public ArrayList<Sprite> elementSprites = new ArrayList<Sprite>();
	public Texture[] numbers = new Texture[10];
	public Texture n1;
	public Texture n2;
	public Texture n3;
	public Texture n4;
	public Texture n5;
	public Texture n6;
	public Texture n7;
	public Texture n8;
	public Texture n9;
	public Texture n0;

//	public Sprite s1;
//	public Sprite s2;
//	public Sprite s3;
//	public Sprite s4;
//	public Sprite s5;
//	public Sprite s6;
//	public Sprite s7;
//	public Sprite s8;
//	public Sprite s9;
//	public Sprite s0;

	public Texture bonusTexture;
	public Sprite bonusSprite;
	
	public Texture scoreTex;
	public Sprite scoreSpr;

	public int halfElement;





	public void onCreate() {
		
		createEngine("graphics/loading.jpg", 480, 320, true);

		this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

		display =  ((WindowManager) this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		int displayHeight = display.getHeight();
		int displayWidth = display.getWidth();
		bonus = new Bonus(51-delay,8,displayHeight,displayWidth);
		snake = new Snake(10,displayHeight,displayWidth);
		game=new Game(this,snake, delay, bonus);
		game.score=0;



	}

	@Override
	public void onLoad() {
		atlas = new TextureAtlas(512, 1024);
		atlas.insert(backgroundTexture = new Texture("graphics/background.png"));
		atlas.insert(elementTexture = new Texture("graphics/element.png"));
		atlas.insert(bonusTexture = new Texture("graphics/bonus3.png"));
		atlas.insert(scoreTex = new Texture("graphics/score/scoreT.png"));

		atlas.insert(n1 = new Texture("graphics/score/1.png"));
		atlas.insert(n2 = new Texture("graphics/score/2.png"));
		atlas.insert(n3 = new Texture("graphics/score/3.png"));
		atlas.insert(n4 = new Texture("graphics/score/4.png"));
		atlas.insert(n5 = new Texture("graphics/score/5.png"));
		atlas.insert(n6 = new Texture("graphics/score/6.png"));
		atlas.insert(n7 = new Texture("graphics/score/7.png"));
		atlas.insert(n8 = new Texture("graphics/score/8.png"));
		atlas.insert(n9 = new Texture("graphics/score/9.png"));
		atlas.insert(n0 = new Texture("graphics/score/0.png"));



		TextureManager.load(atlas);

		background = new FixedBackground(backgroundTexture);
		elementSprite = new Sprite(80, 180, elementTexture);
		bonusSprite = new Sprite(150, 30, bonusTexture);
		scoreSpr = new Sprite(10,7,scoreTex);

		numbers[0] = n0;
		numbers[1] = n1;
		numbers[2] = n2;
		numbers[3] = n3;
		numbers[4] = n4;
		numbers[5] = n5;
		numbers[6] = n6;
		numbers[7] = n7;
		numbers[8] = n8;
		numbers[9] = n9;


		Paint mPaint = new Paint();
		mPaint.setStrokeWidth(5); 
		mPaint.setTextSize(16);
		mPaint.setColor(Color.WHITE);
		mPaint.setAntiAlias(true);
		mPaint.setFakeBoldText(true);
		rokon.initVibrator();
	}


	@Override
	public void onLoadComplete() {
		rokon.setBackground(background);
		rokon.addSprite(scoreSpr);
		rokon.addSprite(bonusSprite);

		halfElement = (int) (elementSprite.getWidth()/2);



	}


	@Override
	public void onTouch(int x, int y, boolean hotspot) {
		game.setDirections(x,y);
	}

	@Override
	public void onPause() {
		super.onPause();
	}



	@Override
	public void onGameLoop() {		
		game.update();
		if(snake.getSnake().size() > elementSprites.size()){
			newESprite();
			rokon.vibrate(100);
			//change score here
		}
		int i = 0;

		for(Sprite e : elementSprites){
			e.setXY((float)snake.getSnake().get(i).x-halfElement,
					(float)snake.getSnake().get(i).y-halfElement);
			i++;
		}

		if(game.hasScoreChanged){
			Log.d("snake", "score:"+game.score);
			addNewScoreToRokon();
		}
		if(game.isBonusadded) {
			SnakeElement bonus = game.bonus.bonus;
			bonusSprite.setXY((float)bonus.x-(bonusSprite.getWidth()/2), (float)bonus.y-(bonusSprite.getHeight()/2));
		}else{
			bonusSprite.setXY(500,500);
		}
		if(game.isOver){
			AlertDialog.Builder dialog = new AlertDialog.Builder(this);
			dialog.setIcon(R.drawable.icon);
			dialog.setTitle("Spacey Snake");  
			dialog.setMessage("Your score is " + game.score);
			dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {  
				public void onClick(DialogInterface dialog, int whichButton) {}  
			});
			dialog.create().show();
			FingerSnake.Graphicends = true;
			FingerSnake.score = game.score;
			finish();

		}


	}


	public void updateScoreList(){
		String score = game.score+"";
		for(Sprite n : scorelist){
			rokon.removeSprite(n);
		}
		
		scorelist.clear();
		int letterdist = 15;
		for(int i=0; i< score.length(); i++){
			scorelist.add(
					new Sprite(
							(scoreTex.getWidth()-5)+i*letterdist,
							7,
							numbers[Integer.parseInt(""+score.charAt(i))]));
		}
		
	}

	public void addNewScoreToRokon(){
		updateScoreList();	
		for(Sprite n : scorelist){
			rokon.addSprite(n);
		}
	}

	@Override
	public void onResume() {
		rokon.unfreeze();
		rokon.unpause();
		super.onResume();
	}

	public void newESprite(){
		SnakeElement e = snake.getSnake().get(snake.getSnake().size()-1);
		elementSprites.add(new Sprite((float)e.x, (float)e.y, elementTexture));
		rokon.addSprite(elementSprites.get(elementSprites.size()-1));
	}


	public double circeDistanze(double x1, double y1, double r1, double x2, double y2, double r2){
		double difX = x2 - x1;
		double difY = y2 - y1;
		return Math.sqrt(difX*difX + difY*difY);
	}
}
